package Sort;

import java.util.Stack;

public class Sort {

    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    //array[j + 1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public void shellSort(int[] array) {
        int gap = array.length / 2;
        while (gap > 1) {
            gap /= 2;
            shell(array , gap);
        }
    }
    public void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    //array[j + 1] = tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    public void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    public void selectSort2(int[] array) {
        int left = 0;
        int right = array.length -1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]){
                    maxIndex = i;
                }
            }
            swap(array,left,minIndex);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

    public void heapSort(int[] array) {
        if (array == null || array.length <= 1) return;

        // 1. 构建大顶堆（初始时end=数组长度）
        createBigHeap(array, array.length);

        // 2. 逐个将堆顶元素（最大值）交换到末尾
        for (int end = array.length - 1; end > 0; end--) {
            swap(array, 0, end);      // 堆顶换到末尾
            siftDown(0, array, end);  // 重新调整剩余部分
        }
    }

    private void createBigHeap(int[] array, int end) {
        // 从最后一个非叶子节点开始倒序调整
        for (int parent = (end - 1) / 2; parent >= 0; parent--) {
            siftDown(parent, array, end);
        }
    }

    private void siftDown(int parent, int[] array, int end) {
        int child = 2 * parent + 1; // 左孩子
        while (child < end) {
            // 如果有右孩子且比左孩子大，选右孩子
            if (child + 1 < end && array[child + 1] > array[child]) {
                child++;
            }
            // 如果孩子比父大，交换并继续向下检查
            if (array[child] > array[parent]) {
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break; // 调整完成
            }
        }
    }

    public void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if (!flg) {
                break;
            }
        }
    }
    public void swap(int[] array,int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }
    public void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
//        int par = partitionHoare(array,start,end);
        int par = partition(array,start,end);
        quick(array,start,par-1);
        quick(array,par+1,end);
    }
    public int partitionHoare(int[] array, int left, int right) {
        int i = left;
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }
    public int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    public void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length -1;
        int par = partition(array,left,right);
        if (par > left + 1) {
            stack.push(left);
            stack.push(par - 1);
        }
        if (par < right - 1) {
            stack.push(par + 1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            par = partition(array,left,right);
            if (par > left + 1) {
                stack.push(left);
                stack.push(par - 1);
            }
            if (par < right - 1) {
                stack.push(par + 1);
                stack.push(right);
            }
        }
    }

    public void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length-1);
    }
    public void mergeSortFun(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid + 1,right);
        //合并
        merge(array,left,mid,right);
    }

    public void merge(int[] array, int left, int mid, int right) {
        int[] tmp = new int[right - left + 1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            } else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }

    //非递归 实现 归并排序
    public void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left + gap - 1;
                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if (right >= array.length) {
                    right = array.length - 1;
                }
                merge(array,left,mid,right);
            }
            gap = gap * 2;
        }
    }


    public void countSort(int[] array) {
        int maxVal = array[0];
        int minVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if (maxVal < array[i]) {
                maxVal = array[i];
            }
            if (minVal > array[i]) {
                minVal = array[i];
            }
        }
        int[] count = new int[maxVal - minVal + 1];
        for (int i = 0; i < array.length; i++) {
            int val = array[i] - minVal;
            count[val]++;
        }
        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index] = i + minVal;
                index++;
                count[i]--;
            }
        }
    }

}














